package com.liukai.bishe.presenter.loginAndRegisterPresenter.impl;

import android.graphics.drawable.Drawable;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.liukai.bishe.model.API;
import com.liukai.bishe.model.doman.user.User;
import com.liukai.bishe.model.doman.user.UserMessage;
import com.liukai.bishe.presenter.loginAndRegisterPresenter.ILoginPresenter;
import com.liukai.bishe.utils.RetrofitManager;
import com.liukai.bishe.utils.ToastUtil;
import com.liukai.bishe.view.IBaseCallBack;
import com.liukai.bishe.view.ILoginCallBack;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class LoginPresenterImpl implements ILoginPresenter {

    ILoginCallBack mILoginCallBack = null;
    private Retrofit mRetrofit;

    @Override
    public void login(User user) {

        mRetrofit = RetrofitManager.getInstance().getRetrofit();
        String userString = JSON.toJSONString(user);
        ToastUtil.log("LoginPresenterImpl","userString",userString);
        API api = mRetrofit.create(API.class);
        Call<UserMessage> task = api.login(userString);
        ToastUtil.log("LoginPresenterImpl","发起请求","");
        task.enqueue(new Callback<UserMessage>() {
            @Override
            public void onResponse(Call<UserMessage> call, Response<UserMessage> response) {

                int code = response.code();
                if (code == HttpURLConnection.HTTP_OK){
//                    ToastUtil.log("LoginPresenterImpl","code",code+"");
                    User user1 = response.body().getUser();
                    ToastUtil.log("LoginPresenterImpl","登录成功",response.body().toString());
                    if(user1.getUserhead() != null && !user1.getUserhead().equals("")){
//                        Constants.userHeadDrawable = loadImageFromNetwork(user1.getUserhead());
                    }
//                    Constants.userHeadDrawable = loadImageFromNetwork(user1.getUserhead());
                    mILoginCallBack.result(response.body());
                }

//                mILoginCallBack.result(response.body());
            }

            @Override
            public void onFailure(Call<UserMessage> call, Throwable t) {
                ToastUtil.log("LoginPresenterImpl","onFailure",t.toString());
            }
        });


    }

    public Drawable loadImageFromNetwork(String imageUrl) {
        Drawable drawable = null;
        try {
            // 可以在这里通过文件名来判断，是否本地有此图片
            drawable = Drawable.createFromStream(
                    new URL(imageUrl).openStream(), "image.jpg");
        } catch (IOException e) {
            Log.d("test", e.getMessage());
        }
        if (drawable == null) {
            Log.d("test", "null drawable");
        } else {
            Log.d("test", "not null drawable");
        }

        return drawable ;
    }


    @Override
    public void registerCallback(IBaseCallBack iBaseCallBack) {

        mILoginCallBack = (ILoginCallBack) iBaseCallBack;


    }

    @Override
    public void unregisterCallback(IBaseCallBack iBaseCallBack) {

        if (mILoginCallBack != null) {
            mILoginCallBack = null;
        }

    }
}
